package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.Owner.AddOwner;
import com.sand.dto.Owner.OwnerDto;
import com.sand.dto.Select.UserSelect;
import com.sand.entity.user.Merchant;
import com.sand.entity.user.Owner;
import com.sand.entity.user.User;
import com.sand.feign.UserFeign;
import com.sand.mapper.OwnerMapper;
import com.sand.owner.Intentionality;
import com.sand.result.AppHttpCodeEnum;
import com.sand.result.ResponseResult;
import com.sand.service.OwnerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OwnerServiceImpl extends ServiceImpl<OwnerMapper, Owner> implements OwnerService {


    @Autowired
    private UserFeign userFeign;


    /**
     * 查询客户
     *
     * @param userSelect
     * @return
     */
    @Override
    public OwnerDto select(UserSelect userSelect) {
        Integer id = userSelect.getId();

        String phone = userSelect.getPhone();

        //两个都没传入
        if (id == null && phone.isEmpty()){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入要查寻的用户id或号码");
        }

        //两个都传入
        if (id != null && !phone.isEmpty()) {

            //根据商家表id查询
            Owner owner = getById(id);
            if (owner == null){
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该客户不存在");
            }

            //根据电话号码获取人员信息
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));
            if (inquire.getData() == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该电话号码不存在");
            }

            return getOwnerDto(inquire, owner);

        }

        //只传入id
        if (id != null){
            //根据商家表id查询
            Owner owner = getById(id);
            if (owner == null){
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该客户不存在");
            }

            //获得客户的userid
            Integer userid = owner.getUserid();

            // 根据userid查询客户基本信息
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(userid, null));

            //拼接
            return getOwnerDto(inquire, owner);
        }

        //只传了电话号码
        ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));

        //根据电话号码获取人员信息
        if (inquire.getData() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该电话号码不存在");
        }

        //获取人员信息中的id
        Integer Uid = inquire.getData().getId();

        //根据id找到客户
        Owner owner = getOne(new LambdaQueryWrapper<Owner>().eq(Owner::getUserid, Uid));

        //拼接
        return getOwnerDto(inquire, owner);
    }

    /**
     * 新增客户
     *
     * @param addOrIntentionality
     * @return
     */
    @Override
    public String addOwner(AddOwner addOwner) {

        //获取id
        Integer userid = addOwner.getUserid();

        //获取意向度
        Integer intentionality = addOwner.getIntentionality();

        if (userid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入用户id");
        }

        //根据id查询用户
        User user = userFeign.getById(userid).getData();


        if (intentionality == null){
            //没有传入意向度默认意向度一般
            addOwner.setIntentionality(Intentionality.BAN);
        }

        if (user == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不存在该用户");
        }

        //保存
        Owner owner = new Owner();

        owner.setUserid(userid);
        owner.setIntentionality(intentionality);

        save(owner);

        return "新增客户成功";
    }




    /**
     * 用于拼接客户和人员信息
     * @param inquire
     * @return
     */
    private static OwnerDto getOwnerDto(ResponseResult<User> inquire, Owner owner) {
        OwnerDto ownerDto = new OwnerDto();

        BeanUtils.copyProperties(inquire.getData(),ownerDto);
        BeanUtils.copyProperties(owner,ownerDto);

        return ownerDto;
    }
}
